package com.dinree.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.CompoundButton;

import com.base.library.BaseActivity;
import com.base.library.widget.CompetitionWebChromeClient;
import com.dinree.R;
import com.dinree.bean.UserBean;
import com.dinree.databinding.ActivitySearchWebBinding;
import com.dinree.utils.Constant;
import com.jakewharton.rxbinding2.view.RxView;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.base.library.Constant.WINDOW_DURATION;

public class SearchWebActivity extends BaseActivity {
    private ActivitySearchWebBinding activitySearchWebBinding;
    private UserBean userBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_web;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activitySearchWebBinding = getViewDataBinding();
        userBean = realm.findFirst(UserBean.class);
        String url = getIntent().getStringExtra("url");
        String key = getIntent().getStringExtra("key");
        activitySearchWebBinding.webView.addJavascriptInterface(new JSHook(), "dinree");
        if (userBean != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("user_id", userBean.user_id);
            map.put("user_token", userBean.user_token);
            map.put("referer", "https://web.dinree.cn/epsea/front");
            activitySearchWebBinding.webView.loadUrl(url, map);
        } else {
            activitySearchWebBinding.webView.loadUrl(url);
        }
        activitySearchWebBinding.webView.setWebChromeClient(new CompetitionWebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    activitySearchWebBinding.webView.loadUrl("javascript:search('" + key + "')");
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissionsCallback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }

            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                super.onShowCustomView(view, customViewCallback);
            }
        });
        activitySearchWebBinding.btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activitySearchWebBinding.webView.loadUrl("javascript:switchDisplay('menu')");
                } else {
                    activitySearchWebBinding.webView.loadUrl("javascript:switchDisplay('grid')");
                }
            }
        });
        RxView.clicks(activitySearchWebBinding.btnBack).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            finish();
        });
        RxView.clicks(activitySearchWebBinding.tvSearch).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, SearchActivity.class);
            intent.putExtra("url", Constant.WEB_HOST + "front/#/search");
            startActivity(intent);
        });
    }

    public class JSHook {
        @JavascriptInterface
        public void close() {
            finish();
        }
        @JavascriptInterface
        public void setDisplay(String type) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (type.equals("menu")) {
                        activitySearchWebBinding.btnSwitch.setChecked(true);
                    }
                    if (type.equals("grid")) {
                        activitySearchWebBinding.btnSwitch.setChecked(false);
                    }
                }
            });
        }
    }
}

